# -*- coding:utf-8 -*-

import asyncio


async def num(n):
    try:
        await asyncio.sleep(n * 0.1)
        return n
    except asyncio.CancelledError:
        print(f"数字{n}被取消")
        raise


async def main():
    tasks = [num(i) for i in range(10)]
    # complete, pending = await asyncio.wait(tasks, timeout=0.7)  #无序输出，超时后需要任务
    complete = await asyncio.gather(*tasks)  # 按顺序输入输出
    for i in complete:
        print("当前数字", i)
    #
    # for i in complete:
    #     print("当前数字", i.result())
    # if pending:
    #     print("取消未完成的任务")
    #     for p in pending:
    #         p.cancel()


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(main())
    finally:
        loop.close()